package com.example.cinemaorder.service.impl;

import com.alibaba.fastjson2.JSON;
import com.example.cinemaorder.entity.SysMovie;
import com.example.cinemaorder.service.SysMovieService;
import com.example.cinemaorder.service.SysRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 高垚淼
 * @version 1.0
 */
@Service
public class SysRankServiceImpl implements SysRankService {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SysMovieService sysMovieService;

    @Override
    public Object getRank1() {
        // 获取所有的电影id遍历
        List<Long> ids = (List<Long>) sysMovieService.getIds();
        ArrayList<SysMovie> sysMovies = new ArrayList<>();
        //遍历取出所有的id和价格信息
        for (int id = 0 ;id<ids.size();id++) {
            SysMovie sysMovie = (SysMovie) redisTemplate.opsForValue().get("电影排行榜："+ids.get(id));
            sysMovies.add(sysMovie);
        }
        //使用比较器根据售票价格排序
        Collections.sort(sysMovies, (o1, o2) -> (int) (o2.getMovieBoxOffice() - o1.getMovieBoxOffice()));
        List<SysMovie> subSysMovies = sysMovies.subList(0, 10);
        return subSysMovies;
    }
}
